package modalities;

import java.awt.event.KeyListener;
import java.awt.event.MouseListener;
import java.util.Observable;
import java.util.Observer;

import barrel_kong.GameController;

/**
 * Interact between the inputs (phidgets and Voice) and give instructions to GameController
 *
 */
public class Decider implements Observer {

	Keyboard keyboard;
	Mouse mouse;
	Phidgets slider;
	Voice playerVoice;
	ObservableThing controllerInterface;
	GameController chief;
	Double currentPowerLevel = 0.0;
	VoiceLevelManager voiceDecrease;


	public Decider(double minHeight, double maxHeight, double minAngle, double maxAngle, GameController chief) {

		this.slider = new Phidgets(0,100, "Phidget Slider", this);
		this.mouse = new Mouse(this);
		this.keyboard = new Keyboard(this);
		this.playerVoice = new Voice("VoiceManager", this);
		this.controllerInterface = new ObservableThing(chief);
		this.chief = chief;
		
		voiceDecrease = new VoiceLevelManager(chief);
		voiceDecrease.start();
		
		this.playerVoice.setLevelActivationState(true);
		this.playerVoice.setVoiceRecogActivationState(true);
		
	}

	Double lastPressureDetected = 0.0;

	//Called when a modality communicate a new Value
	@Override
	public void update(Observable o, Object arg) {

		Message m = (Message)arg;

	
		if(m.getPressureDetected()!=null)
		{
			lastPressureDetected = m.getPressure();
			controllerInterface.externalSetChanged();
			controllerInterface.notifyObservers(arg);
		}
		else if(m.getWordHeard()!=null && lastPressureDetected > 100 && m.getWordHeard().equals("shoot"))
		{
			Message fusionVoicePressure = new Message(true, "shoot");
			controllerInterface.externalSetChanged();
			controllerInterface.notifyObservers(fusionVoicePressure);
			lastPressureDetected = 0.0;
		}
		else if(m.getWordHeard()!=null && lastPressureDetected > 100 && m.getWordHeard().equals("hit"))
		{
			Message fusionVoicePressure = new Message(true, "hit");
			controllerInterface.externalSetChanged();
			controllerInterface.notifyObservers(fusionVoicePressure);
			lastPressureDetected = 0.0;
		}
		/**
		 * When user level is detected over the activation threshold, each
		 * seconds of recording make the level increase. When the sound
		 * is not activates anymore, the level decrease quickly, but not instantly.
		 */
		else if(m.getVoicePower()!=null)
		{
			currentPowerLevel = m.getVoicePower();	
			voiceDecrease.setCurrentValue(currentPowerLevel);

		}
		else
		{
			controllerInterface.externalSetChanged();
			controllerInterface.notifyObservers(arg);
		}
	}
	//Getters for mouse and keyboard
	public KeyListener getKeyListener() 	{ return keyboard;}
	public MouseListener getMouseListener()	{ return mouse;}
	public Voice getplayerVoice()					{ return this.playerVoice;}
	public Voice getLevelListener()					{ return this.playerVoice;}

}
